<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python基础语言 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>python是动态语言 | python基础语言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../01day/section.1.3.html" />
    
    
    <link rel="prev" href="../01day/section.1.0.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="1.2"
        data-chapter-title="python是动态语言"
        data-filepath="01day/section.1.1.md"
        data-basepath=".."
        data-revision="Fri Mar 17 2017 08:48:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="01day/index.html">
            
                
                    <a href="../01day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="01day/section.1.0.html">
            
                
                    <a href="../01day/section.1.0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        元类
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.2" data-path="01day/section.1.1.html">
            
                
                    <a href="../01day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        python是动态语言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="01day/section.1.3.html">
            
                
                    <a href="../01day/section.1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        __slots__
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="02day/section.4.html">
            
                
                    <a href="../02day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        生成器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="02day/section.3.html">
            
                
                    <a href="../02day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        迭代器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="02day/section.1.html">
            
                
                    <a href="../02day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="02day/section.2.html">
            
                
                    <a href="../02day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="02day/index.html">
            
                
                    <a href="../02day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="03day/section.0.2.html">
            
                
                    <a href="../03day/section.0.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="03day/section.1.1.html">
            
                
                    <a href="../03day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        循环导入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="03day/section.0.3.html">
            
                
                    <a href="../03day/section.0.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        作用域
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="03day/section.2.html">
            
                
                    <a href="../03day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        ==、is
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="03day/section.1.html">
            
                
                    <a href="../03day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        深拷贝、浅拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="03day/section.6.html">
            
                
                    <a href="../03day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        进制、位运算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="01day/section.3.html">
            
                
                    <a href="../01day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        私有化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="01day/section.3.1.html">
            
                
                    <a href="../01day/section.3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        属性property
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="03day/index.html">
            
                
                    <a href="../03day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="03day/section.0.1.html">
            
                
                    <a href="../03day/section.0.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        垃圾回收(一)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="03day/section.0.1.1.html">
            
                
                    <a href="../03day/section.0.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        垃圾回收(二)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="03day/section.0.1.2.html">
            
                
                    <a href="../03day/section.0.1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        垃圾回收(三)-gc模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="01day/section.4.html">
            
                
                    <a href="../01day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        内建属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="01day/section.6.html">
            
                
                    <a href="../01day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        内建函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="01day/section.7.html">
            
                
                    <a href="../01day/section.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        集合set
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="02day/section.5.html">
            
                
                    <a href="../02day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        functools
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="03day/section.4.html">
            
                
                    <a href="../03day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="03day/section.3.html">
            
                
                    <a href="../03day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        调试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="03day/section.5.html">
            
                
                    <a href="../03day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        编码风格
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python基础语言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="python&#x662F;&#x52A8;&#x6001;&#x8BED;&#x8A00;">python&#x662F;&#x52A8;&#x6001;&#x8BED;&#x8A00;</h1>
<h3 id="1-&#x52A8;&#x6001;&#x8BED;&#x8A00;&#x7684;&#x5B9A;&#x4E49;">1. &#x52A8;&#x6001;&#x8BED;&#x8A00;&#x7684;&#x5B9A;&#x4E49;</h3>
<p><code>&#x52A8;&#x6001;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;</code> &#x662F; <code>&#x9AD8;&#x7EA7;&#x7A0B;&#x5E8F;&#x8BBE;&#x8BA1;&#x8BED;&#x8A00;</code> &#x7684;&#x4E00;&#x4E2A;&#x7C7B;&#x522B;&#xFF0C;&#x5728;&#x8BA1;&#x7B97;&#x673A;&#x79D1;&#x5B66;&#x9886;&#x57DF;&#x5DF2;&#x88AB;&#x5E7F;&#x6CDB;&#x5E94;&#x7528;&#x3002;&#x5B83;&#x662F;&#x4E00;&#x7C7B; &#x5728;<code>&#x8FD0;&#x884C;&#x65F6;&#x53EF;&#x4EE5;&#x6539;&#x53D8;&#x5176;&#x7ED3;&#x6784;</code>&#x7684;&#x8BED;&#x8A00; &#xFF1A;&#x4F8B;&#x5982;&#x65B0;&#x7684;&#x51FD;&#x6570;&#x3001;&#x5BF9;&#x8C61;&#x3001;&#x751A;&#x81F3;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x88AB;&#x5F15;&#x8FDB;&#xFF0C;&#x5DF2;&#x6709;&#x7684;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x88AB;&#x5220;&#x9664;&#x6216;&#x662F;&#x5176;&#x4ED6;&#x7ED3;&#x6784;&#x4E0A;&#x7684;&#x53D8;&#x5316;&#x3002;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#x76EE;&#x524D;&#x975E;&#x5E38;&#x5177;&#x6709;&#x6D3B;&#x529B;&#x3002;&#x4F8B;&#x5982;JavaScript&#x4FBF;&#x662F;&#x4E00;&#x4E2A;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#xFF0C;&#x9664;&#x6B64;&#x4E4B;&#x5916;&#x5982; PHP &#x3001; Ruby &#x3001; Python &#x7B49;&#x4E5F;&#x90FD;&#x5C5E;&#x4E8E;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#xFF0C;&#x800C; C &#x3001; C++ &#x7B49;&#x8BED;&#x8A00;&#x5219;&#x4E0D;&#x5C5E;&#x4E8E;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#x3002;----&#x6765;&#x81EA; &#x7EF4;&#x57FA;&#x767E;&#x79D1;</p>
<h3 id="2-&#x8FD0;&#x884C;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x7ED9;&#x5BF9;&#x8C61;&#x7ED1;&#x5B9A;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;">2. &#x8FD0;&#x884C;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x7ED9;&#x5BF9;&#x8C61;&#x7ED1;&#x5B9A;(&#x6DFB;&#x52A0;)&#x5C5E;&#x6027;</h3>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name = None, age = None)</span>:</span>
        self.name = name
        self.age = age


<span class="hljs-prompt">&gt;&gt;&gt; </span>P = Person(<span class="hljs-string">&quot;&#x5C0F;&#x660E;&quot;</span>, <span class="hljs-string">&quot;24&quot;</span>)
&gt;&gt;&gt;
</code></pre>
<p>&#x5728;&#x8FD9;&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;1&#x4E2A;&#x7C7B;Person&#xFF0C;&#x5728;&#x8FD9;&#x4E2A;&#x7C7B;&#x91CC;&#xFF0C;&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x4E2A;&#x521D;&#x59CB;&#x5C5E;&#x6027;name&#x548C;age&#xFF0C;&#x4F46;&#x662F;&#x4EBA;&#x8FD8;&#x6709;&#x6027;&#x522B;&#x554A;&#xFF01;&#x5982;&#x679C;&#x8FD9;&#x4E2A;&#x7C7B;&#x4E0D;&#x662F;&#x4F60;&#x5199;&#x7684;&#x662F;&#x4E0D;&#x662F;&#x4F60;&#x4F1A;&#x5C1D;&#x8BD5;&#x8BBF;&#x95EE;&#x6027;&#x522B;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x5462;&#xFF1F;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>P.sex = <span class="hljs-string">&quot;male&quot;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>P.sex
<span class="hljs-string">&apos;male&apos;</span>
&gt;&gt;&gt;
</code></pre>
<p>&#x8FD9;&#x65F6;&#x5019;&#x5C31;&#x53D1;&#x73B0;&#x95EE;&#x9898;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x91CC;&#x9762;&#x6CA1;&#x6709;sex&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x554A;&#xFF01;&#x600E;&#x4E48;&#x56DE;&#x4E8B;&#x5462;&#xFF1F; &#x8FD9;&#x5C31;&#x662F;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#x7684;&#x9B45;&#x529B;&#x548C;&#x5751;&#xFF01; &#x8FD9;&#x91CC; &#x5B9E;&#x9645;&#x4E0A;&#x5C31;&#x662F; &#x52A8;&#x6001;&#x7ED9;&#x5B9E;&#x4F8B;<span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">&#x7ED1;&#x5B9A;&#x5C5E;&#x6027;</span>&#xFF01;</p>
<h3 id="3-&#x8FD0;&#x884C;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x7ED9;&#x7C7B;&#x7ED1;&#x5B9A;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;">3. &#x8FD0;&#x884C;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x7ED9;&#x7C7B;&#x7ED1;&#x5B9A;(&#x6DFB;&#x52A0;)&#x5C5E;&#x6027;</h3>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>P1 = Person(<span class="hljs-string">&quot;&#x5C0F;&#x4E3D;&quot;</span>, <span class="hljs-string">&quot;25&quot;</span>)
<span class="hljs-prompt">&gt;&gt;&gt; </span>P1.sex

Traceback (most recent call last):
  File <span class="hljs-string">&quot;&lt;pyshell#21&gt;&quot;</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
    P1.sex
AttributeError: Person instance has no attribute <span class="hljs-string">&apos;sex&apos;</span>
&gt;&gt;&gt;
</code></pre>
<p>&#x6211;&#x4EEC;&#x5C1D;&#x8BD5;&#x6253;&#x5370;P1.sex&#xFF0C;&#x53D1;&#x73B0;&#x62A5;&#x9519;&#xFF0C;P1&#x6CA1;&#x6709;sex&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#xFF01;---- &#x7ED9;P&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#x7ED1;&#x5B9A;&#x5C5E;&#x6027;&#x5BF9;P1&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#x4E0D;&#x8D77;&#x4F5C;&#x7528;&#xFF01;
&#x90A3;&#x6211;&#x4EEC;&#x8981;&#x7ED9;&#x6240;&#x6709;&#x7684;Person&#x7684;&#x5B9E;&#x4F8B;&#x52A0;&#x4E0A; sex&#x5C5E;&#x6027;&#x600E;&#x4E48;&#x529E;&#x5462;&#xFF1F; &#x7B54;&#x6848;&#x5C31;&#x662F;&#x76F4;&#x63A5;&#x7ED9;Person&#x7ED1;&#x5B9A;&#x5C5E;&#x6027;&#xFF01;</p>
<pre><code class="lang-python">&gt;&gt;&gt;&gt; Person.sex = <span class="hljs-keyword">None</span> <span class="hljs-comment">#&#x7ED9;&#x7C7B;Person&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>P1 = Person(<span class="hljs-string">&quot;&#x5C0F;&#x4E3D;&quot;</span>, <span class="hljs-string">&quot;25&quot;</span>)
<span class="hljs-prompt">&gt;&gt;&gt; </span>print(P1.sex) <span class="hljs-comment">#&#x5982;&#x679C;P1&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x4E2D;&#x6CA1;&#x6709;sex&#x5C5E;&#x6027;&#x7684;&#x8BDD;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x4F1A;&#x8BBF;&#x95EE;&#x5B83;&#x7684;&#x7C7B;&#x5C5E;&#x6027;</span>
<span class="hljs-keyword">None</span> <span class="hljs-comment">#&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x6CA1;&#x6709;&#x51FA;&#x73B0;&#x5F02;&#x5E38;</span>
&gt;&gt;&gt;
</code></pre>
<h3 id="4-&#x8FD0;&#x884C;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x7ED9;&#x7C7B;&#x7ED1;&#x5B9A;&#x6DFB;&#x52A0;&#x65B9;&#x6CD5;">4. &#x8FD0;&#x884C;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x7ED9;&#x7C7B;&#x7ED1;&#x5B9A;(&#x6DFB;&#x52A0;)&#x65B9;&#x6CD5;</h3>
<p>&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x7ED9;Person&#x7ED1;&#x5B9A;sex&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#xFF0C;&#x91CD;&#x65B0;&#x5B9E;&#x4F8B;&#x5316;P1&#x540E;&#xFF0C;P1&#x5C31;&#x6709;sex&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x4E86;&#xFF01;
&#x90A3;&#x4E48;function&#x5462;&#xFF1F;&#x600E;&#x4E48;&#x7ED1;&#x5B9A;&#xFF1F;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name = None, age = None)</span>:</span>
        self.name = name
        self.age = age
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">eat</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&quot;eat food&quot;</span>)


<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span><span class="hljs-params">(self, speed)</span>:</span>
    print(<span class="hljs-string">&quot;%s&#x5728;&#x79FB;&#x52A8;, &#x901F;&#x5EA6;&#x662F; %d km/h&quot;</span>%(self.name, speed))


<span class="hljs-prompt">&gt;&gt;&gt; </span>P = Person(<span class="hljs-string">&quot;&#x8001;&#x738B;&quot;</span>, <span class="hljs-number">24</span>)
<span class="hljs-prompt">&gt;&gt;&gt; </span>P.eat()
eat food
<span class="hljs-prompt">&gt;&gt;&gt; </span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>P.run()
Traceback (most recent call last):
  File <span class="hljs-string">&quot;&lt;pyshell#5&gt;&quot;</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
    P.run()
AttributeError: Person instance has no attribute <span class="hljs-string">&apos;run&apos;</span>
&gt;&gt;&gt;
&gt;&gt;&gt;
<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> types
<span class="hljs-prompt">&gt;&gt;&gt; </span>P.run = types.MethodType(run, P)
<span class="hljs-prompt">&gt;&gt;&gt; </span>P.run(<span class="hljs-number">180</span>)
&#x8001;&#x738B;&#x5728;&#x79FB;&#x52A8;,&#x901F;&#x5EA6;&#x662F; <span class="hljs-number">180</span> km/h
</code></pre>
<p>&#x65E2;&#x7136;&#x7ED9;&#x7C7B;&#x6DFB;&#x52A0;&#x65B9;&#x6CD5;&#xFF0C;&#x662F;&#x4F7F;&#x7528;<code>&#x7C7B;&#x540D;.&#x65B9;&#x6CD5;&#x540D; = xxxx</code>&#xFF0C;&#x90A3;&#x4E48;&#x7ED9;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x4E5F;&#x662F;&#x7C7B;&#x4F3C;&#x7684;<code>&#x5BF9;&#x8C61;.&#x65B9;&#x6CD5;&#x540D; = xxxx</code></p>
<h5 id="&#x5B8C;&#x6574;&#x7684;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;">&#x5B8C;&#x6574;&#x7684;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</h5>
<pre><code class="lang-python">

<span class="hljs-keyword">import</span> types

<span class="hljs-comment">#&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x7C7B;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span><span class="hljs-params">(object)</span>:</span>
    num = <span class="hljs-number">0</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name = None, age = None)</span>:</span>
        self.name = name
        self.age = age
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">eat</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&quot;eat food&quot;</span>)

<span class="hljs-comment">#&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span><span class="hljs-params">(self, speed)</span>:</span>
    print(<span class="hljs-string">&quot;%s&#x5728;&#x79FB;&#x52A8;, &#x901F;&#x5EA6;&#x662F; %d km/h&quot;</span>%(self.name, speed))

<span class="hljs-comment">#&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7C7B;&#x65B9;&#x6CD5;</span>
<span class="hljs-decorator">@classmethod</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">testClass</span><span class="hljs-params">(cls)</span>:</span>
    cls.num = <span class="hljs-number">100</span>

<span class="hljs-comment">#&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x9759;&#x6001;&#x65B9;&#x6CD5;</span>
<span class="hljs-decorator">@staticmethod</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">testStatic</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&quot;---static method----&quot;</span>)

<span class="hljs-comment">#&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;</span>
P = Person(<span class="hljs-string">&quot;&#x8001;&#x738B;&quot;</span>, <span class="hljs-number">24</span>)
<span class="hljs-comment">#&#x8C03;&#x7528;&#x5728;class&#x4E2D;&#x7684;&#x65B9;&#x6CD5;</span>
P.eat()

<span class="hljs-comment">#&#x7ED9;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</span>
P.run = types.MethodType(run, P)
<span class="hljs-comment">#&#x8C03;&#x7528;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</span>
P.run(<span class="hljs-number">180</span>)

<span class="hljs-comment">#&#x7ED9;Person&#x7C7B;&#x7ED1;&#x5B9A;&#x7C7B;&#x65B9;&#x6CD5;</span>
Person.testClass = testClass
<span class="hljs-comment">#&#x8C03;&#x7528;&#x7C7B;&#x65B9;&#x6CD5;</span>
print(Person.num)
Person.testClass()
print(Person.num)

<span class="hljs-comment">#&#x7ED9;Person&#x7C7B;&#x7ED1;&#x5B9A;&#x9759;&#x6001;&#x65B9;&#x6CD5;</span>
Person.testStatic = testStatic
<span class="hljs-comment">#&#x8C03;&#x7528;&#x9759;&#x6001;&#x65B9;&#x6CD5;</span>
Person.testStatic()
</code></pre>
<h3 id="5-&#x8FD0;&#x884C;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x5220;&#x9664;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;">5. &#x8FD0;&#x884C;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x5220;&#x9664;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;</h3>
<p>&#x5220;&#x9664;&#x7684;&#x65B9;&#x6CD5;:</p>
<ol>
<li>del &#x5BF9;&#x8C61;.&#x5C5E;&#x6027;&#x540D;</li>
<li>delattr(&#x5BF9;&#x8C61;, &quot;&#x5C5E;&#x6027;&#x540D;&quot;)</li>
</ol>
<p>&#x901A;&#x8FC7;&#x4EE5;&#x4E0A;&#x4F8B;&#x5B50;&#x53EF;&#x4EE5;&#x5F97;&#x51FA;&#x4E00;&#x4E2A;&#x7ED3;&#x8BBA;&#xFF1A;&#x76F8;&#x5BF9;&#x4E8E;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#xFF0C;&#x9759;&#x6001;&#x8BED;&#x8A00;&#x5177;&#x6709;&#x4E25;&#x8C28;&#x6027;&#xFF01;&#x6240;&#x4EE5;&#xFF0C;&#x73A9;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C0F;&#x5FC3;&#x52A8;&#x6001;&#x7684;&#x5751;&#xFF01;</p>
<p>&#x90A3;&#x4E48;&#x600E;&#x4E48;&#x907F;&#x514D;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x5462;&#xFF1F; &#x8BF7;&#x4F7F;&#x7528;__slots__&#xFF0C;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../01day/section.1.0.html" class="navigation navigation-prev " aria-label="Previous page: 元类"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../01day/section.1.3.html" class="navigation navigation-next " aria-label="Next page: __slots__"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python基础语言 -->
</html>
